﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OPMS_V1.BOL;
using OPMS_V1.DOL;
using System.Data;

namespace OPMS_V1.Public
{
    public partial class PreviewAd : System.Web.UI.Page
    {
        //Instantiating the Advertisement Class
        Advertisement advertise = new Advertisement();
        DataSet ds = new DataSet();
        //Instantiating the Linq Class
        S_PDataContext dc = new S_PDataContext();

        #region Landlord's Personal Details Properties

        /// <summary>
        /// Auto Implemented property for LandlordID
        /// </summary>
        public string LandLordID
        { get; set; }
        /// <summary>
        /// Auto Implemented property for FirstName
        /// </summary>
        public string FirstName
        { get; set; }
        /// <summary>
        /// Auto Implemented property for LastName
        /// </summary>
        public string LastName
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Sex
        /// </summary>
        public string Sex
        { get; set; }
        /// <summary>
        /// Auto Implemented property for DOB
        /// </summary>
        public string DOB
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Phone
        /// </summary>
        public string Phone
        { get; set; }
        /// <summary>
        /// Auto Implemented property for StreetNum
        /// </summary>
        public string StreetNum
        { get; set; }
        /// <summary>
        /// Auto Implemented property for StreetName
        /// </summary>
        public string StreetName
        { get; set; }
        /// <summary>
        /// Auto Implemented property for PostCode
        /// </summary>
        public string PostCode
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Area
        /// </summary>
        public string Area
        { get; set; }
        /// <summary>
        /// Auto Implemented property for State
        /// </summary>
        public string State
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Email
        /// </summary>
        public string Email
        { get; set; }
        /// <summary>
        /// Auto Implemented property for LoginID
        /// </summary>
        public string LoginID
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Existance of User
        /// </summary>
        public string RecordExists
        { get; set; }

        #endregion LandLord's Personal Details Properties

        #region Home Details Properties

        /// <summary>
        /// Auto Implemented property for Bedroom
        /// </summary>
        public string Bedroom
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Bathroom
        /// </summary>
        public string Bathroom
        { get; set; }
        /// <summary>
        /// Auto Implemented property for FloorLevel
        /// </summary>
        public string FloorLevel
        { get; set; }
        /// <summary>
        /// Auto Implemented property for HouseSize
        /// </summary>
        public string HouseSize
        { get; set; }
        /// <summary>
        /// Auto Implemented property for yearBuilt
        /// </summary>
        public string yearBuilt
        { get; set; }

        #endregion Home Detail Properties

        #region Land Detail Properties

        /// <summary>
        /// Auto Implemented property for Number of Homes
        /// </summary>
        public string NumHomes
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Frontage Size
        /// </summary>
        public string Frontage
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Depth Size
        /// </summary>
        public string Depth
        { get; set; }
        /// <summary>
        /// Auto Implemented property for StreetFacing
        /// </summary>
        public string StreetFacing
        { get; set; }
        /// <summary>
        /// Auto Implemented property for StreetWidth
        /// </summary>
        public string StreetWidth
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Number of Size of Land
        /// </summary>
        public string Sides
        { get; set; }

        #endregion Properties

        #region Properties for Property Details
        /// <summary>
        /// Auto Implemented property for PropertyID
        /// </summary>
        public string PropertyID
        { get; set; }
        /// <summary>
        /// Auto Implemented property for PropertyName
        /// </summary>
        public string PropertyName
        { get; set; }
        /// <summary>
        /// Auto Implemented property for PropertyNo
        /// </summary>
        public string PropertyNo
        { get; set; }
        /// <summary>
        /// Auto Implemented property for StreetNumber
        /// </summary>
        public string AdStreetNum
        { get; set; }
        /// <summary>
        /// Auto Implemented property for StreetName
        /// </summary>
        public string AdStreetName
        { get; set; }
        /// <summary>
        /// Auto Implemented property for AreaID
        /// </summary>
        public string AreaID
        { get; set; }
        /// <summary>
        /// Auto Implemented property for FixedPrice
        /// </summary>
        public string FixedPrice
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Price Range: From
        /// </summary>
        public string PriceRangeFrom
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Price Range: To
        /// </summary>
        public string PriceRangeTo
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Bond
        /// </summary>
        public string Bond
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Property Type
        /// </summary>
        public string PropertyType
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Dwelling Details
        /// </summary>
        public string Dwelling
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Property Heading
        /// </summary>
        public string Heading
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Property Description
        /// </summary>
        public string Description
        { get; set; }
        /// <summary>
        /// Auto Implemented property for First Name
        /// </summary>
        public string ContactFName
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Last Name
        /// </summary>
        public string ContactLName
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Contact Method
        /// </summary>
        public string ContactMethod
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Contact Email
        /// </summary>
        public string ContactEmail
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Contact Phone
        /// </summary>
        public string ContactPhone
        { get; set; }
        /// <summary>
        /// Auto Implemented property for Property Availability Status
        /// </summary>
        public string AvailbilityStatus
        { get; set; }

        #endregion Properties for Property Details

        /// <summary>
        /// Autom Implemeted property for Inventory ID
        /// </summary>
        public string InventoryID
        { get; set; }

        /// <summary>
        /// This function is used for the page load event
        /// In this function all the initialisation of the session variables are being made
        /// The image uploaded by the user is shown here
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //Getting the Session variable values to the AutoImplemented Properties
            lblCompleted.Visible = false;
            //Assigning values from session to the variables
            lblError.Visible = false;
            LandLordID = Session["LogLandLordID"].ToString();
            FirstName = Session["LFname"].ToString();
            LastName = Session["LLname"].ToString();
            Sex = Session["Lsex"].ToString();
            DOB = Session["LDOB"].ToString();
            Phone = Session["LPhone"].ToString();
            StreetNum = Session["LStreetNum"].ToString();
            StreetName = Session["LStreetName"].ToString();
            PostCode = Session["LPostCode"].ToString();
            Area = Session["LArea"].ToString();
            State = Session["LState"].ToString();
            Email = Session["LEmail"].ToString();
            LoginID = Session["user"].ToString();

            //Showing the Image Uploaded
            imgPreview.ImageUrl = "~/propImages/" + Session["TaskID"].ToString() + "/" + "1.jpg";

            //Checking for the available sessions
            if (Session["Area"].Equals("") && Session["PostCode"].Equals("") && Session["State"].Equals(""))
            {
                //If no session available then redirect to SelfService
                Session["statusPage"] = "Ad";
                Response.Redirect("~\\Public\\SelfService.aspx?Mode=Ad");
            }
            try
            {
                //Show the address details
                lnkAddress.Text = Session["PropertyName"].ToString() + " " + Session["PropertyNo"].ToString() + " " +
                        Session["StreetName"].ToString() + " " + Session["StreetNo"].ToString() + " " + Session["Area"].ToString() + ", " +
                        Session["PostCode"].ToString() + ", " + Session["State"].ToString();
                //Show the heading
                lblHeadingResult.Text = Session["Heading"].ToString();
                lblHeadingSummary.Text = lblHeadingResult.Text;

                txtDescription.Enabled = false;

                //Display the description if it is available
                if (Session["Description"].ToString() != "")
                {
                    pnlDescriptionDetails.Visible = true;
                    txtDescription.Text = Session["Description"].ToString();
                }
                else
                {
                    pnlDescriptionDetails.Visible = false;
                }

                //Display the Dwelling Details
                lblDwelling.Text = Session["Dwellings"].ToString();
                //Display the Status result
                lblStatus.Text = Session["PropertyStatus"].ToString();
                //Display the Property ID
                lblPropertyID.Text = Session["propertyID"].ToString();

                //Display the Fixed Price if available
                if (!Session["FixedPrice"].Equals(""))
                {
                    lblPrice.Text = Session["FixedPrice"].ToString();
                }
                else
                {
                    //Shoe the price range
                    lblPrice.Text = Session["PriceRangeFrom"].ToString() + ", " + Session["PriceRangeTo"].ToString();
                }

                //Display the Property Type
                lblPropertyType.Text = Session["PropertyType"].ToString();

                //Display the Suburb or Area
                lblSuburb.Text = Session["Area"].ToString();
                //Display the State
                lblState.Text = Session["State"].ToString();

                //If the Property type is other than Land i.e. House, Villa, etc.
                if (lblPropertyType.Text != "Land")
                {
                    pnlLand.Visible = false;
                    pnlHouse.Visible = true;
                    //Display the No. of Bedrooms
                    lblBedroom.Text = Session["BedroomNo"].ToString();
                    //Display the Number of Bathrooms
                    lblBathroom.Text = Session["BathroomNo"].ToString();
                    //Display the no. of floors
                    lblFloors.Text = Session["FloorNo"].ToString();
                    //Display the House Size
                    lblSize.Text = Session["HouseSize"].ToString();

                    //If user entered Year Built 
                    if (Session["YearBuilt"].Equals(null))
                    {
                        pnlYearbuilt.Visible = false;
                    }
                    else
                    {
                        lblyearBuilt.Text = Session["YearBuilt"].ToString();
                    }
                }
                //If the property type is Land
                else
                {
                    pnlHouse.Visible = false;
                    pnlLand.Visible = true;
                    pnlLand.Style.Add("top", "133px;");
                    //Display the Number of Homes
                    lblHomeNo.Text = Session["NumberHomes"].ToString();

                    //Display the number of Sides
                    if (Session["Standard"].ToString() != "" || Session["Standard"].ToString() != null)
                    {
                        lblSides.Text = Session["Standard"].ToString();
                    }
                    else
                    {
                        lblSides.Text = Session["NonStandard"].ToString();
                    }
                    //Display the Frontage Size
                    lblFrontage.Text = Session["Frontage"].ToString();
                    //Display the Depth Size
                    lblDepth.Text = Session["Depth"].ToString();
                    //Display the Street Facing of the land
                    lblStreetFacing.Text = Session["StreetFacing"].ToString();
                    //Display the Street Width
                    lblStreetWidth.Text = Session["StreetWidth"].ToString();
                }
                //Display the Contact Name (Advertiser)
                lblName.Text = Session["FirstName"].ToString() + " " + Session["LastName"].ToString();
                //Display the Contact method of advertiser
                lblMethod.Text = Session["ContactMethod"].ToString();
                //Display the Email address of advertiser
                lblEmail.Text = Session["Email"].ToString();
                //Display the contact number of Advertiser
                lblPhone.Text = Session["Phone"].ToString();

                lblAgents.Text = "Causual";
            }
            //if any error occurs then Redirect to error page
            catch (Exception)
            {
                Response.Redirect("~//ErrorHandling//ErrorPage.aspx");
            }
        }

        /// <summary>
        /// This function is used for Storing the Final path and then
        /// will be used to pass in the value to the Database
        /// </summary>
        /// <returns></returns>
        protected Boolean createImageFolder()
        {
            //Getting the Task ID from the Session[....] variable
            String TaskId = Session["TaskID"].ToString();
            String propImagesLOC = Server.MapPath("../propImages");
            String sourceFolderLOC = propImagesLOC + "\\" + Session["TaskID"].ToString();
            String propID = Session["propertyID"].ToString();

            String pathImage01, pathImage02, pathImage03, pathImage04, pathImage05;
            //propImages/prop65805387/1.jpg
            pathImage01 = "/propImages/" + propID + "/1.jpg";
            pathImage02 = "/propImages/" + propID + "/2.jpg";
            pathImage03 = "/propImages/" + propID + "/3.jpg";
            pathImage04 = "/propImages/" + propID + "/4.jpg";
            pathImage05 = "/propImages/" + propID + "/5.jpg";

            //Moving the temporary directory created with the Property ID
            if (System.IO.Directory.Exists(sourceFolderLOC))
            {
                System.IO.Directory.Move(sourceFolderLOC, propImagesLOC + "//" + Session["propertyID"]);
                //System.IO.Directory.Delete(tempFolderLOC);
            }
            //Displaying the Image after moving to the appropriate dictory structure
            imgPreview.ImageUrl = "~/propImages/" + propID + "/" + "1.jpg";

            //Inserting the Image path to the database
            dc.Insert_ImagePath(@propID, @pathImage01, @pathImage02, @pathImage03, @pathImage04, @pathImage05);

            return true;
        }

        /// <summary>
        /// This function is used for the actual insertion of the Records.
        /// These records will be inserted only if there are appropriate results in the form.
        /// Once the form is displayed with the appropriate result, the user can submit the form.
        /// It stores the Information such as "Registration Details", "Personal Details", "Land Details",
        /// "Home Details", "Inventory Details"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmitAd_Click(object sender, EventArgs e)
        {
            Boolean successful = false;
            string randomInventoryID;
            //Checking for the existance of the property ID, 
            //Implemented for the Editing Purpose
            if (Session["propertyID"].ToString() == "" || Session["propertyID"].ToString() == null)
            {
                string randomProperty;
                //Generate the Random Property ID
                do
                {
                    //Generating the Random Task ID which is not present in Database
                    randomProperty = advertise.RandomProperty();
                } while (advertise.checkPropertyId(randomProperty));

                //Creating the session of the property ID
                Session["propertyID"] = "prop" + randomProperty;

                //If inventory session is not available then create the inventoryID
                if (Session["InventoryID"].ToString().Equals(""))
                {
                    do
                    {
                        //Generating the Random Task ID which is not present in Database
                        randomInventoryID = advertise.RandomInventoryID();
                    } while (advertise.checkInventoryID(randomInventoryID));
                    Session["InventoryID"] = "Inv" + randomInventoryID;
                    InventoryID = Session["InventoryID"].ToString();
                }
                //If inventory ID is present
                else
                {
                    InventoryID = Session["InventoryID"].ToString();
                }

                RecordExists = Session["recordExists"].ToString();
                //If the Landlord personal details not exists in the database 
                if (RecordExists.Equals("false"))
                {
                    try
                    {
                        //Insert the records into the database
                        //It inserts such as "LandlordID, login ID, FName, LName, etc.
                        dc.Insert_LandlordPDetails(@LandLordID, @LoginID, @FirstName, @LastName, @Sex, @DOB, @Phone, @StreetNum, @StreetName, @PostCode, @Area, @State, @Email);
                    }
                    catch (Exception)
                    {
                        lblError.Visible = true;
                    }
                }
                try
                {
                    //Storing the Property ID from the session
                    PropertyID = Session["propertyID"].ToString();

                    //If the property type selection is Land 
                    if (lblPropertyType.Text != "Land")
                    {
                        //This function is used for Assigning the values to the property Details.
                        // This function is called when the User presses the Submit Button in order to 
                        // confirm the Ad.
                        GetDetails();

                        //Getting the No. of Bedrooms
                        Bedroom = Session["BedroomNo"].ToString();
                        //Getting the No. of Bethrooms
                        Bathroom = Session["BathroomNo"].ToString();
                        //Getting the No. of Floors
                        FloorLevel = Session["FloorNo"].ToString();
                        //Getting the Size of House
                        HouseSize = Session["HouseSize"].ToString();

                        //Checking for details in NonReqired feild, Year Built
                        if (Session["YearBuilt"].ToString().Equals(""))
                        {
                            yearBuilt = null;
                        }
                        else
                        {
                            yearBuilt = Session["YearBuilt"].ToString();
                        }

                        //This function is used for the image creation, once the property Ad has been confirmrd
                        createImageFolder();

                        //Once the required details are fetched then, Insert the Property Home Ad.
                        //It inserts the values such as PropertyID, LandLordID, PropertyNo, StreetNo., 
                        //Price Details, Property Details
                        dc.Insert_HomeDetails(@PropertyID, @LandLordID, @PropertyName, @PropertyNo,
                            @AdStreetNum, @AdStreetName, @AreaID, @FixedPrice, @PriceRangeFrom,
                            @PriceRangeTo, @Bond, @PropertyType, @Bedroom, @Bathroom, @FloorLevel,
                            @HouseSize, @yearBuilt, @AvailbilityStatus, @Dwelling, @Heading,
                            @Description, @ContactFName, @ContactLName, @ContactMethod,
                            @ContactEmail, @ContactPhone);

                        //Setting the Status as Completed.
                        string AdStatus = "Completed";
                        //Insert the Landlord inventory details
                        dc.Insert_LandLordInventory(@InventoryID, @LandLordID, @PropertyID, @AdStatus, @PropertyType);
                        Session["propertyID"] = "";
                    }
                    //If the property type is Land
                    else
                    {
                        //This function is used for Assigning the values to the property Details.
                        // This function is called when the User presses the Submit Button in order to 
                        // confirm the Ad.
                        GetDetails();

                        //Getting the number of Homes in the Property
                        if (Session["NumberHomes"].ToString().Equals(""))
                        {
                            NumHomes = null;
                        }
                        else
                        {
                            NumHomes = Session["NumberHomes"].ToString();
                        }

                        //Getting the Number of Sides of the Property
                        if (Session["Standard"].ToString() != "")
                        {
                            Sides = Session["Standard"].ToString();
                        }
                        else
                        {
                            Sides = Session["NonStandard"].ToString();
                        }
                        //Getting the Frontage Size of the Property
                        Frontage = Session["Frontage"].ToString();
                        //Getting the Depth Size of the Property
                        Depth = Session["Depth"].ToString();
                        //Getting the StreetFacing of the Property
                        StreetFacing = Session["StreetFacing"].ToString();

                        //Getting the StreetWidth Size of the Property
                        if (Session["StreetWidth"].ToString().Equals("0"))
                        {
                            StreetWidth = null;
                        }
                        else
                        {
                            StreetWidth = Session["StreetWidth"].ToString();
                        }

                        try
                        {
                            //This function is used fore creating the Images for the Property with the 
                            //Property ID
                            createImageFolder();

                            //Once the required details are fetched then, Insert the Property Land Ad.
                            //It inserts the values such as PropertyID, LandLordID, PropertyNo, StreetNo., 
                            //Price Details, Property Details
                            dc.Insert_LandDetails(@PropertyID, @LandLordID, @PropertyName, @PropertyNo, @AdStreetNum, @AdStreetName,
                                @AreaID, @FixedPrice, @PriceRangeFrom, @PriceRangeTo, @Bond, @PropertyType, @NumHomes,
                                @Frontage, @Depth, @StreetFacing, @StreetWidth, @AvailbilityStatus, @Sides, @Dwelling,
                                @Heading, @Description, @ContactFName, @ContactLName, @ContactMethod, @ContactEmail,
                                @ContactPhone);
                            string AdStatus = "Completed";
                            //Add the Inventory for the Property Address
                            dc.Insert_LandLordInventory(@InventoryID, @LandLordID, @PropertyID, @AdStatus, @PropertyType);
                            successful = true;
                        }
                        catch (Exception)
                        {
                            successful = false;
                        }
                        if (successful == true)
                        {
                            lblCompleted.Visible = true;
                            Session["propertyID"] = "";
                        }
                    }
                }
                catch (Exception se)
                {
                    lblError.Visible = true;
                    string s = se.Message;
                }
            }
            //If the Property ID exists then only update the records...
            //This option will be executed only when the user have chosen an option to edit a property
            else
            {
                //Getting the Property ID
                PropertyID = Session["propertyID"].ToString();
                //Getting the Inventory ID
                ds = advertise.Get_InventoryID(PropertyID);
                //Getting the LandLord ID
                LandLordID = Session["LogLandLordID"].ToString();

                InventoryID = ds.Tables[0].Rows[0][0].ToString();
                Session["InventoryID"] = InventoryID;

                //Checking for the existance of Records
                RecordExists = Session["recordExists"].ToString();
                //If the Landlord personal details not exists in the database 
                if (RecordExists.Equals("false"))
                {
                    try
                    {
                        //Insert the records into the database
                        //It Inserts such as "LandlordID, login ID, FName, LName, etc.
                        dc.Insert_LandlordPDetails(@LandLordID, @LoginID, @FirstName, @LastName, @Sex, @DOB, @Phone, @StreetNum, @StreetName, @PostCode, @Area, @State, @Email);
                    }
                    catch (Exception)
                    {
                        lblError.Visible = true;
                    }
                }
                try
                {
                    //Storing the Property ID from the session
                    PropertyID = Session["propertyID"].ToString();

                    //If the property type selection is Land 
                    if (lblPropertyType.Text != "Land")
                    {
                        //This function is used for Assigning the values to the property Details.
                        // This function is called when the User presses the Submit Button in order to 
                        // confirm the Ad.
                        GetDetails();

                        //Getting the No. of Bedrooms
                        Bedroom = Session["BedroomNo"].ToString();
                        //Getting the No. of Bethrooms
                        Bathroom = Session["BathroomNo"].ToString();
                        //Getting the No. of Floors
                        FloorLevel = Session["FloorNo"].ToString();
                        //Getting the Size of House
                        HouseSize = Session["HouseSize"].ToString();

                        //Checking for details in NonReqired feild, Year Built
                        if (Session["YearBuilt"].ToString().Equals(""))
                        {
                            yearBuilt = null;
                        }
                        else
                        {
                            yearBuilt = Session["YearBuilt"].ToString();
                        }

                        //This function is used for the image creation, once the property Ad has been confirmrd
                        createImageFolder();

                        //Once the required details are fetched then, Update the Property Home Ad.
                        //It updates the values such as PropertyID, LandLordID, PropertyNo, StreetNo., 
                        //Price Details, Property Details
                        dc.Update_Home_Details(@PropertyID, @LandLordID, @PropertyName, @PropertyNo,
                            @AdStreetNum, @AdStreetName, @AreaID, @FixedPrice, @PriceRangeFrom,
                            @PriceRangeTo, @Bond, @PropertyType, @Bedroom, @Bathroom, @FloorLevel,
                            @HouseSize, @yearBuilt, @AvailbilityStatus, @Dwelling, @Heading,
                            @Description, @ContactFName, @ContactLName, @ContactMethod,
                            @ContactEmail, @ContactPhone);

                        //Setting the Status as Completed.
                        string AdStatus = "Completed";
                        //Update the Landlord inventory details
                        dc.Update_Inventory_Details(@LandLordID, @PropertyID, @AdStatus, @PropertyType);
                        Session["propertyID"] = "";
                    }
                    else
                    {
                        //This function is used for Assigning the values to the property Details.
                        // This function is called when the User presses the Submit Button in order to 
                        // confirm the Ad.
                        GetDetails();

                        //Getting the Number of Homes in a land
                        if (Session["NumberHomes"].ToString().Equals(""))
                        {
                            NumHomes = null;
                        }
                        else
                        {
                            NumHomes = Session["NumberHomes"].ToString();
                        }

                        //Getting the Number of sides of a land (Standard or Non-Standard)
                        if (Session["Standard"].ToString() != "")
                        {
                            Sides = Session["Standard"].ToString();
                        }
                        else
                        {
                            Sides = Session["NonStandard"].ToString();
                        }

                        //Getting teh Frontage Size
                        Frontage = Session["Frontage"].ToString();

                        //Getting teh Depth Size
                        Depth = Session["Depth"].ToString();

                        //Getting teh StreetFacing of the Property
                        StreetFacing = Session["StreetFacing"].ToString();

                        //Getting the StreetWidth of the Property
                        if (Session["StreetWidth"].ToString().Equals("0"))
                        {
                            StreetWidth = null;
                        }
                        else
                        {
                            StreetWidth = Session["StreetWidth"].ToString();
                        }

                        try
                        {
                            //This function is used for Creating the Images Directory for the Property
                            createImageFolder();

                            //Once the required details are fetched then, Updates the Property Land Ad.
                            //It updates the values such as PropertyID, LandLordID, PropertyNo, StreetNo., 
                            //Price Details, Property Details
                            dc.Update_Land_Details(@PropertyID, @LandLordID, @PropertyName, @PropertyNo, @AdStreetNum, @AdStreetName,
                                @AreaID, @FixedPrice, @PriceRangeFrom, @PriceRangeTo, @Bond, @PropertyType, @NumHomes,
                                @Frontage, @Depth, @StreetFacing, @StreetWidth, @AvailbilityStatus, @Sides, @Dwelling,
                                @Heading, @Description, @ContactFName, @ContactLName, @ContactMethod, @ContactEmail,
                                @ContactPhone);
                            string AdStatus = "Completed";
                            //Update the Landlord inventory details
                            dc.Update_Inventory_Details(@LandLordID, @PropertyID, @AdStatus, @PropertyType);
                            successful = true;
                        }
                        catch (Exception)
                        {
                            successful = false;
                        }

                        if (successful == true)
                        {
                            lblCompleted.Visible = true;
                        }
                    }
                }
                catch (Exception se)
                {
                    //lblError.Visible = true;
                    string s = se.Message;
                }
            }
        }

        /// <summary>
        /// This function is used for Assigning the values to the property Details.
        /// This function is called when the User presses the Submit Button in order to 
        /// confirm the Ad.
        /// </summary>
        /// <returns></returns>
        #region Get Details (Session variables for Property Details)
        protected Boolean GetDetails()
        {
            //Checking the details in NonRequiredField, Property Name
            if (Session["PropertyName"].ToString().Equals(""))
            {
                PropertyName = null;
            }
            else
            {
                PropertyName = Session["PropertyName"].ToString();
            }
            //Checking for details in NonReqired feild, Property No
            if (Session["PropertyNo"].ToString().Equals(""))
            {
                PropertyNo = null;
            }
            else
            {
                PropertyNo = Session["PropertyNo"].ToString();
            }
            //Checking for details in NonReqired feild, StreetNo
            if (Session["StreetNo"].ToString().Equals(""))
            {
                AdStreetNum = null;
            }
            else
            {
                AdStreetNum = Session["StreetNo"].ToString();
            }

            //Storing the StreetName from the session
            AdStreetName = Session["StreetName"].ToString();
            AreaID = Session["AreaID"].ToString();

            //Checking for details in Nonreqired feild, Fixed Price
            if (Session["FixedPrice"].ToString().Equals(""))
            {
                FixedPrice = null;
            }
            else
            {
                FixedPrice = Session["FixedPrice"].ToString();
            }
            //Checking for details in Nonreqired feild, Price Range: From
            if (Session["PriceRangeFrom"].ToString().Equals(""))
            {
                PriceRangeFrom = null;
            }
            else
            {
                PriceRangeFrom = Session["PriceRangeFrom"].ToString();
            }

            //Checking for details in Nonreqired feild, Price Range: To
            if (Session["PriceRangeTo"].ToString().Equals(""))
            {
                PriceRangeTo = null;
            }
            else
            {
                PriceRangeTo = Session["PriceRangeTo"].ToString();
            }

            //Checking for details in NonReqired feild, Bond Price
            if (Session["Bond"].ToString().Equals(""))
            {
                Bond = null;
            }
            else
            {
                Bond = Session["Bond"].ToString();
            }

            //Getting the Property Type of what the user seleted (Home, LAND, Villa etc.)
            PropertyType = Session["PropertyType"].ToString();
            //Getting the Status on what the property is available
            AvailbilityStatus = Session["PropertyStatus"].ToString();
            //Getting the Dwelling Details
            Dwelling = Session["Dwellings"].ToString();
            //Getting the Property Heading
            Heading = Session["Heading"].ToString();
            //Getting the Property Description
            Description = Session["Description"].ToString();
            //Getting the Contact FirstName
            ContactFName = Session["FirstName"].ToString();
            //Getting the Contact Last Name
            ContactLName = Session["LastName"].ToString();
            //Getting the Contact Method
            ContactMethod = Session["ContactMethod"].ToString();
            //Getting the Contact Email
            ContactEmail = Session["Email"].ToString();
            //Getting the Contact Phone
            ContactPhone = Session["Phone"].ToString();

            return true;
        }
        #endregion Get Details (Session variables for Property Details)


        /// <summary>
        /// This method is used for Saving the property.
        /// This is executed when the user want to save the Property and will be 
        /// editing to complete the Ad
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Boolean successful = false;
            string randomInventoryID;
            //Checking for the existance of the property ID, 
            //Implemented for the Editing Purpose
            if (Session["propertyID"].ToString() == "" || Session["propertyID"].ToString() == null)
            {
                string randomProperty;
                //Generate the Random Property ID
                do
                {
                    //Generating the Random Task ID which is not present in Database
                    randomProperty = advertise.RandomProperty();
                } while (advertise.checkPropertyId(randomProperty));

                //Creating the session of the property ID
                Session["propertyID"] = "prop" + randomProperty;

                //If inventory session is not available then create the inventoryID
                if (Session["InventoryID"].ToString().Equals(""))
                {
                    do
                    {
                        //Generating the Random Task ID which is not present in Database
                        randomInventoryID = advertise.RandomInventoryID();
                    } while (advertise.checkInventoryID(randomInventoryID));
                    Session["InventoryID"] = "Inv" + randomInventoryID;
                    InventoryID = Session["InventoryID"].ToString();
                }
                //If inventory ID is present
                else
                {
                    InventoryID = Session["InventoryID"].ToString();
                }

                RecordExists = Session["recordExists"].ToString();
                //If the Landlord personal details not exists in the database 
                if (RecordExists.Equals("false"))
                {
                    try
                    {
                        //Insert the records into the database
                        //It inserts such as "LandlordID, login ID, FName, LName, etc.
                        dc.Insert_LandlordPDetails(@LandLordID, @LoginID, @FirstName, @LastName, @Sex, @DOB, @Phone, @StreetNum, @StreetName, @PostCode, @Area, @State, @Email);
                    }
                    catch (Exception)
                    {
                        lblError.Visible = true;
                    }
                }
                try
                {
                    //Storing the Property ID from the session
                    PropertyID = Session["propertyID"].ToString();

                    //If the property type selection is Land 
                    if (lblPropertyType.Text != "Land")
                    {
                        //This function is used for Assigning the values to the property Details.
                        // This function is called when the User presses the Submit Button in order to 
                        // confirm the Ad.
                        GetDetails();

                        //Getting the No. of Bedrooms
                        Bedroom = Session["BedroomNo"].ToString();
                        //Getting the No. of Bethrooms
                        Bathroom = Session["BathroomNo"].ToString();
                        //Getting the No. of Floors
                        FloorLevel = Session["FloorNo"].ToString();
                        //Getting the Size of House
                        HouseSize = Session["HouseSize"].ToString();

                        //Checking for details in NonReqired feild, Year Built
                        if (Session["YearBuilt"].ToString().Equals(""))
                        {
                            yearBuilt = null;
                        }
                        else
                        {
                            yearBuilt = Session["YearBuilt"].ToString();
                        }

                        //This function is used for the image creation, once the property Ad has been confirmrd
                        createImageFolder();

                        //Once the required details are fetched then, Insert the Property Home Ad.
                        //It inserts the values such as PropertyID, LandLordID, PropertyNo, StreetNo., 
                        //Price Details, Property Details
                        dc.Insert_HomeDetails(@PropertyID, @LandLordID, @PropertyName, @PropertyNo,
                            @AdStreetNum, @AdStreetName, @AreaID, @FixedPrice, @PriceRangeFrom,
                            @PriceRangeTo, @Bond, @PropertyType, @Bedroom, @Bathroom, @FloorLevel,
                            @HouseSize, @yearBuilt, @AvailbilityStatus, @Dwelling, @Heading,
                            @Description, @ContactFName, @ContactLName, @ContactMethod,
                            @ContactEmail, @ContactPhone);

                        //Setting the Status as Incompleted.
                        string AdStatus = "Incomplete";
                        //Insert the Landlord inventory details
                        dc.Insert_LandLordInventory(@InventoryID, @LandLordID, @PropertyID, @AdStatus, @PropertyType);
                        Session["propertyID"] = "";
                    }
                    //If the property type is Land
                    else
                    {
                        //This function is used for Assigning the values to the property Details.
                        // This function is called when the User presses the Submit Button in order to 
                        // confirm the Ad.
                        GetDetails();

                        //Getting the number of Homes in the Property
                        if (Session["NumberHomes"].ToString().Equals(""))
                        {
                            NumHomes = null;
                        }
                        else
                        {
                            NumHomes = Session["NumberHomes"].ToString();
                        }

                        //Getting the Number of Sides of the Property
                        if (Session["Standard"].ToString() != "")
                        {
                            Sides = Session["Standard"].ToString();
                        }
                        else
                        {
                            Sides = Session["NonStandard"].ToString();
                        }
                        //Getting the Frontage Size of the Property
                        Frontage = Session["Frontage"].ToString();
                        //Getting the Depth Size of the Property
                        Depth = Session["Depth"].ToString();
                        //Getting the StreetFacing of the Property
                        StreetFacing = Session["StreetFacing"].ToString();

                        //Getting the StreetWidth Size of the Property
                        if (Session["StreetWidth"].ToString().Equals("0"))
                        {
                            StreetWidth = null;
                        }
                        else
                        {
                            StreetWidth = Session["StreetWidth"].ToString();
                        }

                        try
                        {
                            //This function is used fore creating the Images for the Property with the 
                            //Property ID
                            createImageFolder();

                            //Once the required details are fetched then, Insert the Property Land Ad.
                            //It inserts the values such as PropertyID, LandLordID, PropertyNo, StreetNo., 
                            //Price Details, Property Details
                            dc.Insert_LandDetails(@PropertyID, @LandLordID, @PropertyName, @PropertyNo, @AdStreetNum, @AdStreetName,
                                @AreaID, @FixedPrice, @PriceRangeFrom, @PriceRangeTo, @Bond, @PropertyType, @NumHomes,
                                @Frontage, @Depth, @StreetFacing, @StreetWidth, @AvailbilityStatus, @Sides, @Dwelling,
                                @Heading, @Description, @ContactFName, @ContactLName, @ContactMethod, @ContactEmail,
                                @ContactPhone);
                            string AdStatus = "Incomplete";
                            //Add the Inventory for the Property Address
                            dc.Insert_LandLordInventory(@InventoryID, @LandLordID, @PropertyID, @AdStatus, @PropertyType);
                            successful = true;
                        }
                        catch (Exception)
                        {
                            successful = false;
                        }
                        if (successful == true)
                        {
                            lblCompleted.Visible = true;
                            Session["propertyID"] = "";
                        }
                    }
                }
                catch (Exception se)
                {
                    lblError.Visible = true;
                    string s = se.Message;
                }
            }
            //If the Property ID exists then only update the records...
            //This option will be executed only when the user have chosen an option to edit a property
            else
            {
                //Getting the Property ID
                PropertyID = Session["propertyID"].ToString();
                //Getting the Inventory ID
                ds = advertise.Get_InventoryID(PropertyID);
                //Getting the LandLord ID
                LandLordID = Session["LogLandLordID"].ToString();

                InventoryID = ds.Tables[0].Rows[0][0].ToString();
                Session["InventoryID"] = InventoryID;

                //Checking for the existance of Records
                RecordExists = Session["recordExists"].ToString();
                //If the Landlord personal details not exists in the database 
                if (RecordExists.Equals("false"))
                {
                    try
                    {
                        //Insert the records into the database
                        //It Inserts such as "LandlordID, login ID, FName, LName, etc.
                        dc.Insert_LandlordPDetails(@LandLordID, @LoginID, @FirstName, @LastName, @Sex, @DOB, @Phone, @StreetNum, @StreetName, @PostCode, @Area, @State, @Email);
                    }
                    catch (Exception)
                    {
                        lblError.Visible = true;
                    }
                }
                try
                {
                    //Storing the Property ID from the session
                    PropertyID = Session["propertyID"].ToString();

                    //If the property type selection is Land 
                    if (lblPropertyType.Text != "Land")
                    {
                        //This function is used for Assigning the values to the property Details.
                        // This function is called when the User presses the Submit Button in order to 
                        // confirm the Ad.
                        GetDetails();

                        //Getting the No. of Bedrooms
                        Bedroom = Session["BedroomNo"].ToString();
                        //Getting the No. of Bethrooms
                        Bathroom = Session["BathroomNo"].ToString();
                        //Getting the No. of Floors
                        FloorLevel = Session["FloorNo"].ToString();
                        //Getting the Size of House
                        HouseSize = Session["HouseSize"].ToString();

                        //Checking for details in NonReqired feild, Year Built
                        if (Session["YearBuilt"].ToString().Equals(""))
                        {
                            yearBuilt = null;
                        }
                        else
                        {
                            yearBuilt = Session["YearBuilt"].ToString();
                        }

                        //This function is used for the image creation, once the property Ad has been confirmrd
                        createImageFolder();

                        //Once the required details are fetched then, Update the Property Home Ad.
                        //It updates the values such as PropertyID, LandLordID, PropertyNo, StreetNo., 
                        //Price Details, Property Details
                        dc.Update_Home_Details(@PropertyID, @LandLordID, @PropertyName, @PropertyNo,
                            @AdStreetNum, @AdStreetName, @AreaID, @FixedPrice, @PriceRangeFrom,
                            @PriceRangeTo, @Bond, @PropertyType, @Bedroom, @Bathroom, @FloorLevel,
                            @HouseSize, @yearBuilt, @AvailbilityStatus, @Dwelling, @Heading,
                            @Description, @ContactFName, @ContactLName, @ContactMethod,
                            @ContactEmail, @ContactPhone);

                        //Setting the Status as Incomplete.
                        string AdStatus = "Incomplete";
                        //Update the Landlord inventory details
                        dc.Update_Inventory_Details(@LandLordID, @PropertyID, @AdStatus, @PropertyType);
                        Session["propertyID"] = "";
                    }
                    else
                    {
                        //This function is used for Assigning the values to the property Details.
                        // This function is called when the User presses the Submit Button in order to 
                        // confirm the Ad.
                        GetDetails();

                        //Getting the Number of Homes in a land
                        if (Session["NumberHomes"].ToString().Equals(""))
                        {
                            NumHomes = null;
                        }
                        else
                        {
                            NumHomes = Session["NumberHomes"].ToString();
                        }

                        //Getting the Number of sides of a land (Standard or Non-Standard)
                        if (Session["Standard"].ToString() != "")
                        {
                            Sides = Session["Standard"].ToString();
                        }
                        else
                        {
                            Sides = Session["NonStandard"].ToString();
                        }

                        //Getting teh Frontage Size
                        Frontage = Session["Frontage"].ToString();

                        //Getting teh Depth Size
                        Depth = Session["Depth"].ToString();

                        //Getting teh StreetFacing of the Property
                        StreetFacing = Session["StreetFacing"].ToString();

                        //Getting the StreetWidth of the Property
                        if (Session["StreetWidth"].ToString().Equals("0"))
                        {
                            StreetWidth = null;
                        }
                        else
                        {
                            StreetWidth = Session["StreetWidth"].ToString();
                        }

                        try
                        {
                            //This function is used for Creating the Images Directory for the Property
                            createImageFolder();

                            //Once the required details are fetched then, Updates the Property Land Ad.
                            //It updates the values such as PropertyID, LandLordID, PropertyNo, StreetNo., 
                            //Price Details, Property Details
                            dc.Update_Land_Details(@PropertyID, @LandLordID, @PropertyName, @PropertyNo, @AdStreetNum, @AdStreetName,
                                @AreaID, @FixedPrice, @PriceRangeFrom, @PriceRangeTo, @Bond, @PropertyType, @NumHomes,
                                @Frontage, @Depth, @StreetFacing, @StreetWidth, @AvailbilityStatus, @Sides, @Dwelling,
                                @Heading, @Description, @ContactFName, @ContactLName, @ContactMethod, @ContactEmail,
                                @ContactPhone);
                            string AdStatus = "Incomplete";
                            //Update the Landlord inventory details
                            dc.Update_Inventory_Details(@LandLordID, @PropertyID, @AdStatus, @PropertyType);
                            successful = true;
                        }
                        catch (Exception)
                        {
                            successful = false;
                        }

                        if (successful == true)
                        {
                            lblCompleted.Visible = true;
                        }
                    }
                }
                catch (Exception se)
                {
                    //lblError.Visible = true;
                    string s = se.Message;
                }
            }
        }
    }
}
